
import UIKit

//MARK: - TextView
extension UITextView {
    
    private struct AssociatedKey {
        /// 默认文本
        static var placeholder              = "textView_placeholder"
        /// 字体大小
        static var fontKey                  = "textView_placeholder_font"
        /// 字体颜色
        static var placeholderColor         = "textView_placeholder_color"
        /// 内边距
        static var placeholderInsets        = "textView_placeholder_insets"
        
        static let placeholderTag           = 984601
    }
    
    /// 文本
    var placeholder: String? {
        get {
            return objc_getAssociatedObject(self, &AssociatedKey.placeholder) as? String ?? ""
        } set {
            objc_setAssociatedObject(self, &AssociatedKey.placeholder, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
            self.makeOrModifyPlaceholder()
        }
    }
    
    /// 文本字体
    var placeholderFont: UIFont? {
        get {
            return objc_getAssociatedObject(self, &AssociatedKey.fontKey) as? UIFont ?? UIFont.systemFont(ofSize: 14)
        } set {
            objc_setAssociatedObject(self, &AssociatedKey.fontKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
            self.makeOrModifyPlaceholder()
        }
    }
    
    /// 文本颜色
    var placeholderColor: UIColor? {
        get {
            return objc_getAssociatedObject(self, &AssociatedKey.placeholderColor) as? UIColor ?? UIColor.lightText
        } set {
            objc_setAssociatedObject(self, &AssociatedKey.placeholderColor, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
            self.makeOrModifyPlaceholder()
        }
    }
    
    // 内边距
    var placeholderInsets: UIEdgeInsets {
        get {
            return objc_getAssociatedObject(self, &AssociatedKey.placeholderInsets) as? UIEdgeInsets ?? UIEdgeInsets(top: 5.0, left: 5.0, bottom: 0.0, right: 5.0)
        } set {
            objc_setAssociatedObject(self, &AssociatedKey.placeholderInsets, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
            self.makeOrModifyPlaceholder()
        }
    }
    
    //TODO: - 创建或者修改 placeholder
    @objc private func makeOrModifyPlaceholder() {
        if self.frame.isEmpty {
            self.perform(#selector(makeOrModifyPlaceholder), with: nil, afterDelay: 0.05)
            return
        }
        // 添加文本监听
        self.addObserverTextChange()
        
        var placeholderLabel: UILabel?
        if let label = self.viewWithTag(AssociatedKey.placeholderTag) as? UILabel {
            placeholderLabel = label
        } else {
            let label = UILabel()
            label.numberOfLines = 0
            label.tag = AssociatedKey.placeholderTag
            self.addSubview(label)
        }
        
        placeholderLabel?.textColor = self.placeholderColor
        placeholderLabel?.font = self.placeholderFont
        let insets = self.placeholderInsets
        placeholderLabel?.frame = CGRect(origin: CGPoint(x: insets.left, y: insets.top), size: CGSize(width: self.frame.width - (insets.left + insets.right), height: 0))
        placeholderLabel?.text = placeholder
        placeholderLabel?.sizeToFit()
    }
    
    /// 文本变化
    func textChange() {
        if let label = self.viewWithTag(AssociatedKey.placeholderTag) as? UILabel {
            label.isHidden = text.count > 0
        } else {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
                self.textChange()
            }
        }
    }
    
    //MARK: - 添加文本变化监听
    private func addObserverTextChange() {
        DispatchQueue.once {
            NotificationCenter.default.addObserver(forName: UITextView.textDidChangeNotification, object: nil, queue: nil) { noti in
                if let textView = noti.object as? UITextView {
                    textView.textChange()
                }
            }
        }
    }
    
}
